package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.system.WxOpenidRefEntity;
import com.dragon.modules.yuegou.mapper.system.WxOpenidRefMapper;
import com.dragon.modules.yuegou.model.system.WxOpenidRef;
import com.dragon.modules.yuegou.query.system.WxOpenidRefQuery;
import com.dragon.modules.yuegou.service.system.WxOpenidRefService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户openid关系表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-05-28 17:49:12
 * @version 2.0
 */
@Service("sysWxOpenidRefService")
public class WxOpenidRefServiceImpl extends ServiceImpl<WxOpenidRefMapper, WxOpenidRefEntity>
		implements WxOpenidRefService {

	@Override
	public WxOpenidRef getByOpenId(String openid, String appId) {
		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setWxOpenId(openid);
		query.setAppid(appId);
		List<WxOpenidRefEntity> list = this.list(query.toQueryWrapper());
		return CollectionUtils.isEmpty(list) ? null : new WxOpenidRef(list.get(0));
	}

	@Override
	@Transactional
	public void createNewRef(WxOpenidRef wxOpenidRef) {
		Check.notNull(wxOpenidRef, "用户关联信息不能为空");
		Check.notEmpty(wxOpenidRef.getUserId(), "用户id不能为空");
		Check.notEmpty(wxOpenidRef.getWxOpenId(), "openid不能为空");
		Check.notNull(wxOpenidRef.getOpenIdType(), "openid类型不能为空");
		Check.notEmpty(wxOpenidRef.getAppid(), "appId类型不能为空");

		WxOpenidRefQuery query = new WxOpenidRefQuery();
		query.setOpenIdType(wxOpenidRef.getOpenIdType());
		query.setWxOpenId(wxOpenidRef.getWxOpenId());
		query.setAppid(wxOpenidRef.getAppid());
		baseMapper.delete(query.toQueryWrapper());

		baseMapper.insert(wxOpenidRef);
	}
}